package com.guigu.buy.service.impl;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.github.pagehelper.util.StringUtil;
import com.guigu.buy.dto.PurOrder_Details;
import com.guigu.buy.dto.PurOrder_Supplier;
import com.guigu.buy.mapper.SupplierMapper;
import com.guigu.buy.pojo.PurchaseOrder;
import com.guigu.buy.mapper.PurchaseOrderMapper;
import com.guigu.buy.pojo.PurchaseOrderDetails;
import com.guigu.buy.pojo.Stock;
import com.guigu.buy.service.IProductSupplierService;
import com.guigu.buy.service.IPurchaseOrderDetailsService;
import com.guigu.buy.service.IPurchaseOrderService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.guigu.buy.vo.PurAllVo;
import com.guigu.buy.vo.PurOrderVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.*;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author com.guigu
 * @since 2024-05-15
 */
@Service
public class PurchaseOrderServiceImpl extends ServiceImpl<PurchaseOrderMapper, PurchaseOrder> implements IPurchaseOrderService {
    @Autowired
    private SupplierMapper supplierMapper;
    @Autowired
    private PurchaseOrderMapper purchaseOrderMapper;
    @Autowired
    private IProductSupplierService productSupplierService;
    @Autowired
    private IPurchaseOrderDetailsService detailsService;

    @Override
    public String getMaxDesignId() {
       PurchaseOrder purchaseOrder= purchaseOrderMapper.queryMaxPurchaseOrder();
       if(purchaseOrder==null){
           return null;
       }
       String designId=purchaseOrder.getPurchaseOrderNumber();
        return designId;
    }

    @Override
    public boolean add(PurOrder_Details purOrder_details,String number) {
        //定义set集合统计供应商编号
        Set<Integer> set=new HashSet();
        Double amount=0.0;
        //遍历前端传回的采购单详情
        System.out.println(purOrder_details);
        for (PurchaseOrderDetails o:purOrder_details.getList()) {
            //利用set集合去重复的供应商编号
            set.add(o.getSupplierId());
            //遍历set集合添加采购单
            Integer num=0;
            for (int supplierId:set){
                if(o.getSupplierId()==supplierId){
                    //计算总数量
                    num+=o.getCount().intValue();
                    //计算总金额
                    amount+=o.getTotal();
                    purOrder_details.setId(null);
                    purOrder_details.setSupplierId(supplierId);
                    purOrder_details.setPurchaseOrderNumber(number);
                    purOrder_details.setPurchaseOrderNum(num);
                    purOrder_details.setPurchaseOrderAmount(amount);
                    purOrder_details.setCheckTag("S001-1");
                    purOrder_details.setPayMethod("P002-1");
                    purOrder_details.setGatherTag("K001-1");
                    purOrder_details.setConveyTag("P001-1");
                    purOrder_details.setCheckTime(purOrder_details.getPurchaseOrderTime());
                    this.save(purOrder_details);
                    for (PurchaseOrderDetails O:purOrder_details.getList()) {
                        if(O.getSupplierId()==supplierId){
                            O.setId(null);
                            O.setParentId(purOrder_details.getId());
                            O.setGatherTag("K001-1");
                            detailsService.save(O);
                        }
                    }
                }
            }
        }
        return true;
    }

    @Override
    public PageInfo<PurOrder_Supplier> selPurOrder(int pageNo, int pageSize, PurOrderVo vo) {
        System.out.println(vo);
        List<PurOrder_Supplier> list=purchaseOrderMapper.selPurOrder(vo);
        PageHelper.startPage(pageSize,pageNo );
        PageInfo<PurOrder_Supplier> pageInfo=new PageInfo<>(list);
        System.out.println(list);
        return pageInfo;
    }

    @Override
    public PageInfo<PurOrder_Supplier> queryAll(int pageNo, int pageSize, PurAllVo vo) {
        List<PurOrder_Supplier> list=purchaseOrderMapper.queryAll(vo);
        PageHelper.startPage(pageSize,pageNo );
        PageInfo<PurOrder_Supplier> pageInfo=new PageInfo<>(list);
        System.out.println(list);
        return pageInfo;
    }

    @Override
    public PageInfo<PurchaseOrder> page(int parentId, int pageNo, int pageSize) {
        //根据用户编号查询供应商编号
        int supplierId=supplierMapper.selSupplierId(parentId);
        //根据供应商编号查询采购订单
        List<PurchaseOrder> list=purchaseOrderMapper.list(supplierId);
        PageHelper.startPage(pageNo,pageSize);
        PageInfo<PurchaseOrder> pageInfo = new PageInfo<>(list);
        return pageInfo;
    }


}
